﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Localization;



namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditDateTime : EditControl
  {
    private bool convertTimezone;
    private EditDate ctlDate;
    private EditTime ctlTime;




    //public EditDateTime()
    //{
    //  base.Init += new EventHandler(this.EditDateTime_Init);
    //}


    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditDateTime_Init);
      this.ctlDate = new EditDate();
      this.ctlDate.Initialise(this.FieldTitle, this.FieldType, this.FieldID, this.ModuleId, this.HelpText, this.DefaultValue, this.Required, this.ValidationRule, this.ValidationMessage, this.Style, this.InputSettings, this.OutputSettings, this.NormalizeFlag, this.FilterTags, this.FilterScript, this.ListInputType, this.ModuleContext);
      this.Controls.Add(this.ctlDate);
      this.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
      Label label2 = new Label();
      label2.Text = string.Format("{0}: ", DotNetNuke.Services.Localization.Localization.GetString("Time", EditControl.LocalResourceFile));
      label2.CssClass = "SubHead";
      Label child = label2;
      this.Controls.Add(child);
      this.ctlTime = new EditTime();
      this.ctlTime.Initialise(this.FieldTitle, this.FieldType, this.FieldID, this.ModuleId, this.HelpText, this.DefaultValue, this.Required, this.ValidationRule, this.ValidationMessage, this.Style, this.InputSettings, this.OutputSettings, this.NormalizeFlag, this.FilterTags, this.FilterScript, this.ListInputType, this.ModuleContext);
      this.Controls.Add(this.ctlTime);
      this.convertTimezone = this.NormalizeFlag;
      this.Value = this.DefaultValue;
    }



    //private void EditDateTime_Init(object sender, EventArgs evt)
    //{
    //  this.ctlDate = new EditDate();
    //  this.ctlDate.Initialise(this.FieldTitle, this.FieldType, this.FieldID, this.ModuleId, this.HelpText, this.DefaultValue, this.Required, this.ValidationRule, this.ValidationMessage, this.Style, this.InputSettings, this.OutputSettings, this.NormalizeFlag, this.FilterTags, this.FilterScript, this.ListInputType, this.ModuleContext);
    //  this.Controls.Add(this.ctlDate);
    //  this.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
    //  Label label2 = new Label();
    //  label2.Text = string.Format("{0}: ", DotNetNuke.Services.Localization.Localization.GetString("Time", EditControl.LocalResourceFile));
    //  label2.CssClass = "SubHead";
    //  Label child = label2;
    //  this.Controls.Add(child);
    //  this.ctlTime = new EditTime();
    //  this.ctlTime.Initialise(this.FieldTitle, this.FieldType, this.FieldID, this.ModuleId, this.HelpText, this.DefaultValue, this.Required, this.ValidationRule, this.ValidationMessage, this.Style, this.InputSettings, this.OutputSettings, this.NormalizeFlag, this.FilterTags, this.FilterScript, this.ListInputType, this.ModuleContext);
    //  this.Controls.Add(this.ctlTime);
    //  this.convertTimezone = this.NormalizeFlag;
    //  this.Value = this.DefaultValue;
    //}




    protected override bool IsValidType()
    {
      bool flag;
      if ( string.IsNullOrEmpty(this.ctlTime.Value) && string.IsNullOrEmpty(this.ctlDate.Value) )
      {
        return true;
      }
      try
      {
        DateTime time = DateTime.ParseExact(this.ctlDate.Value, new string[] { "d", "D", "s" }, Thread.CurrentThread.CurrentCulture, DateTimeStyles.None);
        time = DateTime.ParseExact(this.ctlTime.Value, new string[] { "t", "T", "s" }, Thread.CurrentThread.CurrentCulture, DateTimeStyles.None);
        flag = true;
      }
      catch (Exception e)
      {

        Exception exception = e;
        flag = false;

      }
      return flag;
    }




    public override string Value
    {
      get
      {
        DateTime dt;
        DateTime time2;

        if ((this.ctlDate.Value == "") & (this.ctlTime.Value == ""))
          return "";

        if (!(DateTime.TryParse(this.ctlDate.Value, out dt) && DateTime.TryParse(this.ctlTime.Value, out time2)))
          return string.Format("{0} {1}", this.ctlDate.Value, this.ctlTime.Value);

        //dt = DateTime.Parse(this.ctlDate.Value);
        //time2 = DateTime.Parse(this.ctlTime.Value);

        dt = new DateTime(dt.Year, dt.Month, dt.Day, time2.Hour, time2.Minute, time2.Second);
        if (this.convertTimezone && System.Web.HttpContext.Current.Request.IsAuthenticated)
        {
          UserInfo currentUserInfo = UserController.GetCurrentUserInfo();
          dt = new UserTime().ConvertToServerTime(dt, (double)currentUserInfo.Profile.TimeZone);
        }
        return dt.ToString("s");
      }
      set
      {
        DateTime dt;
        if (DateTime.TryParse(value, out dt))
        {
          if (this.convertTimezone && System.Web.HttpContext.Current.Request.IsAuthenticated)
          {
            UserInfo currentUserInfo = UserController.GetCurrentUserInfo();
            UserTime time2 = new UserTime();
            value = time2.ConvertToUserTime(dt, (double)currentUserInfo.Profile.TimeZone).ToString("s");
          }
        }
        this.ctlDate.Value = value;
        this.ctlTime.Value = value;
      }
    }



  }

}

